package se.energimyndigheten.lampguiden.lighting;

import android.annotation.TargetApi;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import org.json.JSONException;

import java.io.IOException;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.XmlClickable;
import se.energimyndigheten.lampguiden.util.AssetsUtil;

public class RoomGalleryActivity extends FragmentActivity implements XmlClickable {

    public static final String EXTRA_ROOM_ID = "room_id";

    private static final String TAG = RoomGalleryActivity.class.getSimpleName();

    protected RoomPagerAdapter mRoomPagerAdapter;
    protected ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.room_gallery_activity);

        mViewPager = (ViewPager) findViewById(R.id.pager);

        try {
            // get rooms from json
            Room[] rooms = AssetsUtil.getRoomsFromJson(getAssets());

            // extract names to array
            String[] names = new String[rooms.length];
            for (int i = 0, N = rooms.length; i < N; i++) {
                names[i] = rooms[i].getName();
            }

            // update pager and adapter with names
            mRoomPagerAdapter = new RoomPagerAdapter(getSupportFragmentManager(), names);
            mViewPager.setAdapter(mRoomPagerAdapter);


        } catch (JSONException jse) {
            jse.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        ((PagerTabStrip) findViewById(R.id.pager_strip)).setDrawFullUnderline(false);

        // set activem item
        int roomIndex = getIntent().getIntExtra(EXTRA_ROOM_ID, 0);
        mViewPager.setCurrentItem(roomIndex);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // ignore changes to keep in landscape
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onXmlClick(View view) {
        ((XmlClickable) mRoomPagerAdapter.getCurrentFragment()).onXmlClick(view);
    }

    /**
     * Use immersive full screen on sdk KIKKAT (18( and newer
     * @param hasFocus
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}

class RoomPagerAdapter extends FragmentPagerAdapter {
    String[] mRooms;
    Fragment mFrag;
    Fragment mCurrentFragment;

    public RoomPagerAdapter(FragmentManager fm, String[] rooms) {
        super(fm);
        mRooms = rooms;
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new RoomItemFragment();
        Bundle args = new Bundle();
        args.putString(RoomItemFragment.EXTRA_ROOM_NAME, mRooms[i]);
        fragment.setArguments(args);
        mFrag = fragment;
        return fragment;
    }

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (getCurrentFragment() != object) {
            mCurrentFragment = ((Fragment) object);
        }
        super.setPrimaryItem(container, position, object);
    }

    public Fragment getFragment() {
        return mFrag;
    }

    @Override
    public int getCount() {
        return mRooms.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mRooms[position];
    }
}
